package leetcode;
/*
package Test;

*/
/**
 * Created by Ghost on 2014/11/21.
 *//*

public class String_to_Integer {
    public int atoi(String str) {
        if(str==null || str.isEmpty())
            return 0;
        String candidate = str.trim();
        char[] chars = candidate.toCharArray();

        if(chars.length==0)
            return 0;

        int i=0;
        for(;i<chars.length && (chars[i]<'0'||chars[i]>'9') && (chars[i]!='-' && chars[i]!='+') ;i++);
        if(i==chars.length)
            return 0;
        else{
            char flag='+';
            int sum=0;
            for(;i<chars.length;i++){
                if(chars[i]=='-'){
                    flag = '-';
                    sum = -1;
                }else if(chars[i]=='+'){
                }
                else if(chars[i]>='0' && chars[i]<='9'){
                    sum=10*sum+(chars[i]-'0');
                    if(sum<0)
                        return 2147483647;
                }
                else
                    break;
            }
            System.out.println("flag is "+flag);
            if(sum<0)
                return 2147483647;
            else{
                if(flag=='-'){
                    sum = -1* sum;
                    if(sum>0)
                        return -2147483648;
                    else
                        return sum;
                }
                else return sum;
            }

        }
    }
    public static void main(String[] args){
//        int a = -2147483643;
//        int b = a+a-10;
//        System.out.println(b);
        String a = "  dsa  fdsa fds +21474836489fdaf";
        int aa = new String_to_Integer().atoi(a);
        System.out.println(aa);
    }
}
*/
